package xyz.xuminghai.javafx_learn.scene.control;

import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.concurrent.TimeUnit;

/**
 * 2023/5/19 0:54 星期五<br/>
 * 进度条
 *
 * @author xuMingHai
 */
public class ProgressBarApp extends Application {

    private final HBox hBox = new HBox(30.0);

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        Button button = new Button("start");
        ProgressBar progressBar = new ProgressBar(0.0);
        progressBar.setPrefWidth(200.0);
        Label label = new Label("0%");

        Service<Void> service = new Service<>() {

            {
                // 添加进度监听
                progressProperty().addListener((observable, oldValue, newValue) -> {
                    double doubleValue = newValue.doubleValue();
                    if (doubleValue == -1) {
                        progressBar.setProgress(0.0);
                    } else {
                        progressBar.setProgress(doubleValue);
                    }

                });
                // 添加消息监听
                messageProperty().addListener((observable, oldValue, newValue) -> {
                    if ("".equals(newValue)) {
                        label.setText("0%");
                    } else {
                        label.setText(newValue);
                    }
                });
            }

            @Override
            protected Task<Void> createTask() {
                return new Task<>() {
                    @Override
                    protected Void call() {
                        for (int i = 1; i < 101; i++) {
                            if (isCancelled()) {
                                break;
                            }
                            updateProgress((double) i / 100, 1.0);
                            updateMessage(i + "%");
                            try {
                                TimeUnit.MILLISECONDS.sleep(100L);
                            } catch (InterruptedException e) {
                                if (isCancelled()) {
                                    break;
                                }
                            }
                        }
                        return null;
                    }
                };
            }

            @Override
            protected void succeeded() {
                System.out.println("progressBar.getProgress() = " + progressBar.getProgress());
                button.setText("reset");
                button.setOnAction(event -> reset());
            }

            @Override
            protected void running() {
                System.out.println("running");
                button.setText("cancel");
                button.setOnAction(event -> cancel());
            }

            @Override
            protected void cancelled() {
                System.out.println("cancelled");
                button.setText("reset");
                button.setOnAction(event -> reset());
            }

            @Override
            protected void ready() {
                System.out.println("ready");
                button.setText("start");
                button.setOnAction(event -> start());
            }
        };

        button.setOnAction(event -> service.start());

        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(button, progressBar, label);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(hBox, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
